package com.bw.module_message.ui

import android.content.Context
import android.content.Intent
import android.media.AudioManager
import android.os.Build
import android.provider.Settings
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.bw.lib_common.arouter.Router
import com.bw.lib_common.base.BaseFragment
import com.bw.module_message.R
import com.bw.module_message.databinding.FragmentMessageBinding
import com.bw.module_message.viewmodel.MessageViewModel
import dagger.hilt.android.AndroidEntryPoint



@AndroidEntryPoint
@Route(path = Router.Ui.MessageFragment)
class MessageFragment : BaseFragment<MessageViewModel,FragmentMessageBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.fragment_message

    val mViewModel:MessageViewModel by viewModels()

    private lateinit var audioManager: AudioManager

    override fun initView() {

    }

    override fun initData() {
        mViewModel.getMessages1()
        mViewModel.getMessages2()
        mViewModel.getMessages3()
        audioManager = requireContext().getSystemService(Context.AUDIO_SERVICE) as AudioManager
        mBinding.silentVibrateSwitch.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    //isNotificationPolicyAccessGranted
                    if (!audioManager.isMusicActive ) {
                        requestNotificationPolicyAccess()
                    } else {
                        setPhoneToSilentMode()
                    }
                } else {
                    setPhoneToSilentMode()
                }
            } else {
                setPhoneToNormalMode()
            }
        }
    }

    override fun initListener() {
        mViewModel.message1_.observe(this){
            mBinding.msg.text=it[0].messageContent
            mBinding.msgstatus.text=it[0].messageStatus
        }

        mViewModel.message2_.observe(this){
            mBinding.msg2.text=it[0].messageContent
            mBinding.msgstatus2.text=it[0].messageStatus
        }

        mViewModel.message3_.observe(this){
            mBinding.msg3.text=it[0].messageContent
            mBinding.msgstatus3.text=it[0].messageStatus
        }
    }

    private fun requestNotificationPolicyAccess() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            val intent = Intent(Settings.ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS)
            startActivity(intent)
        }
    }

    private fun setPhoneToSilentMode() {
        audioManager.ringerMode = AudioManager.RINGER_MODE_SILENT
        Toast.makeText(requireContext(), "已设置为静音模式", Toast.LENGTH_SHORT).show()
    }

    private fun setPhoneToNormalMode() {
        audioManager.ringerMode = AudioManager.RINGER_MODE_NORMAL
        Toast.makeText(requireContext(), "已恢复正常模式", Toast.LENGTH_SHORT).show()
    }

}